package pers.w.chaptertalk.reader;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import pers.w.chaptertalk.MyService;
import pers.w.chaptertalk.R;
import pers.w.chaptertalk.funs.Fun;
import pers.w.chaptertalk.funs.db.Tab_Book;

class ControlWindow {
    private static final String TAG = "reader.control_window";
    private Button btn_show_menu, btn_next_chapter, btn_pre_chapter,
            btn_refresh_content, btn_read_setting, btn_spoken,btn_cache;
    private CheckBox cb_chapter_talks;
    private LinearLayout layout;// ll_reader_control;
    private ReaderActivity reader;

    ControlWindow(ReaderActivity reader) {
        this.reader = reader;
        layout = reader.findViewById(R.id.ll_reader_control);
        btn_show_menu = reader.findViewById(R.id.btn_show_menu);
        btn_next_chapter = reader.findViewById(R.id.btn_next_chapter);
        btn_pre_chapter = reader.findViewById(R.id.btn_pre_chapter);
        btn_refresh_content = reader.findViewById(R.id.btn_refresh_content);
        btn_read_setting = reader.findViewById(R.id.btn_read_setting);
        btn_spoken = reader.findViewById(R.id.btn_spoken);
        btn_cache = reader.findViewById(R.id.btn_cache_book);

        cb_chapter_talks = reader.findViewById(R.id.cb_chapter_talks);
        setBtns_Listener();
        if (reader.book.getID()<=0)
            btn_cache.setText("加入书架");
    }

    public boolean showChapterTalks(){
        return cb_chapter_talks.isChecked();
    }
    private void setBtns_Listener() {

        View.OnClickListener btn_listener = v -> {
            switch (v.getId()) {
                case R.id.btn_next_chapter:
                    if (reader.chapters.size() > reader.currentChapterIndex + 1)
                        reader.loading_chapter(reader.currentChapterIndex + 1, ReaderControl.BEGIN);
                    break;
                case R.id.btn_pre_chapter:
                    if (0 <= reader.currentChapterIndex - 1)
                        reader.loading_chapter(reader.currentChapterIndex - 1, ReaderControl.BEGIN);
                    break;
                case R.id.btn_refresh_content:
                    reader.chapter.delFile();
                    reader.getContent(ReaderControl.BEGIN);
                    break;
                case R.id.btn_show_menu:
                    reader.sidebar.setVisibility(true);
                    layout.setVisibility(View.GONE);
                    reader.sidebar.lv_chapters.setSelection(reader.currentChapterIndex);
                    break;
                case R.id.btn_read_setting:
                    reader.settingWindow.setVisibility(true);
                    break;
                case R.id.btn_spoken:
                    reader.spoken_setting.startSpoken();
                    this.setVisibility(false);
                    break;
                case R.id.btn_cache_book:
                    switch (btn_cache.getText().toString()){
                        case "全本缓存":
                            if (Fun.isNetworkConnected(reader)) {
                                int add = reader.service.addDownload(reader.book,
                                        reader.chapters.subList(reader.currentChapterIndex,reader.chapters.size()-1));
                                switch(add) {
                                    case   MyService.DOWNLOAD_OK:
                                        btn_cache.setText("停止缓存");
                                        reader.showToask("开始从当前章节开始缓存");
                                        break;
                                    case  MyService.DOWNLOAD_TOO_MUCH:
                                        reader.showToask("不能同时缓存太多，容易卡顿，最多3本");
                                        break;
                                    case  MyService.DOWNLOAD_HAS:
                                        reader.showToask("已经在缓存队列中");
                                        btn_cache.setText("停止缓存");
                                        break;
                                    case MyService.DOWNLOAD_NODATA:
                                        reader.showToask("没有章节信息");
                                        break;
                                }
                            }
                            break;
                        case "停止缓存":
                            btn_cache.setText("全本缓存");
                            reader.service.stopDownload(reader.book.getID());
                            break;
                        case "加入书架":
                            Tab_Book tab_book = new Tab_Book(reader);
                            if (tab_book.add(reader.book,reader.chapters/*chapter_list*/) > 0) {
                                reader.showToask("添加成功");
                                btn_cache.setText("全本缓存");
                                reader.book.saveBmp();
                                Fun.SHARE_MAP.put(R.string.SHARE_BOOKRACK_ADD,reader.book);
                            }
                            break;
                    }
                    break;
            }
        };
        btn_show_menu.setOnClickListener(btn_listener);
        btn_next_chapter.setOnClickListener(btn_listener);
        btn_pre_chapter.setOnClickListener(btn_listener);
        btn_refresh_content.setOnClickListener(btn_listener);
        btn_read_setting.setOnClickListener(btn_listener);
        btn_spoken.setOnClickListener(btn_listener);
        btn_cache.setOnClickListener(btn_listener);

        cb_chapter_talks.setOnCheckedChangeListener((buttonView, isChecked) -> {
            reader.postReFill();
        });
    }

    void setVisibility(boolean visibility) {
        layout.setVisibility(visibility ? View.VISIBLE : View.GONE);
        final Animation alpha1 = AnimationUtils.loadAnimation(reader, R.anim.alpha0_1);
        final Animation alpha0 = AnimationUtils.loadAnimation(reader, R.anim.alpha1_0);

        if (visibility)
            layout.startAnimation(alpha1);
        else layout.startAnimation(alpha0);
    }

    boolean isVisibility() {
        return layout.getVisibility() == View.VISIBLE;
    }

}